package com.greenx.hello.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class HelloDate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // tCalendar();
        // tC2();
        testDateJava();
    }

    /**
     * 
     */
    private final static void testDateJava() {

        Calendar cal = Calendar.getInstance();
        long startTimeMs = System.currentTimeMillis();
        System.out.println("current time:" + cal.getTime().toString() + "  :startTimeMs:" + startTimeMs);

        //test case:1
        Calendar c = Calendar.getInstance();
        Date aDate = c.getTime();
        System.out.println("current aDate:" + aDate);
        c.add(Calendar.DAY_OF_YEAR, 30);
        aDate = c.getTime();
        System.out.println("aDate:plus 30:" + aDate);

        //test case:2
        String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        System.out.print(months[c.get(Calendar.MONTH)]);
        
        //test case:3
        String string = "January 2, 2010";
        Date date;
        try {
            date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        //test case: 4
        long taskTimeS = (System.currentTimeMillis() - startTimeMs);
        System.out.println("endTimeMs:" + System.currentTimeMillis()  + " :taskTimeMs:" + taskTimeS);

    }

    private final static void tC2() {
        String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
        // Create a calendar initialized with the
        // current date and time in the default
        // locale and timezone.
        Calendar calendar = Calendar.getInstance();
        // Display current time and date information.
        // System.out.print("Date: ");
        // System.out.print(months[calendar.get(Calendar.MONTH)]);
        // System.out.print(" " + calendar.get(Calendar.DATE) + " ");
        // System.out.println(calendar.get(Calendar.YEAR));
        // System.out.print("Time: ");
        // System.out.print(calendar.get(Calendar.HOUR) + ":");
        // System.out.print(calendar.get(Calendar.MINUTE) + ":");
        // System.out.println(calendar.get(Calendar.SECOND));
        // Set the time and date information and display it.
        String string = "January 2, 2010";
        Date date;
        try {
            date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);

            System.out.println(date);
            date = new SimpleDateFormat("MMMM d, yyyy").parse(string);
            System.out.println(date.getYear() + "|" + date.getMonth() + "|" + date.getDate());
            calendar.set(2013, 02, 10);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        calendar.set(2013, 02, 10);
        calendar.set(Calendar.HOUR, 10);
        calendar.set(Calendar.MINUTE, 37);
        calendar.set(Calendar.SECOND, 22);
        System.out.print("Updated time: ");
        System.out.print(calendar.get(Calendar.HOUR) + ":");
        System.out.print(calendar.get(Calendar.MINUTE) + ":");
        System.out.println(calendar.get(Calendar.SECOND));
        System.out.println(calendar.getTime().toString());
        Date d = calendar.getTime();
        System.out.println(d.toString());

        calendar = new GregorianCalendar();
        calendar.setTime(d);
        // calendar.add(Calendar.DAY_OF_YEAR, 60-45);
        // calendar.add(Calendar.YEAR, 20);
        System.out.println(calendar.getTime().toString());

    }

    private final static void tCalendar() {
        Calendar c = Calendar.getInstance();
        Date aDate = c.getTime();
        System.out.println("current aDate:" + aDate);
        c.add(Calendar.DAY_OF_YEAR, 30);
        aDate = c.getTime();
        System.out.println("aDate:plus 30:" + aDate);
        c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 45);
        aDate = c.getTime();
        System.out.println("aDate:plus 45:" + aDate);
        c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 60);
        aDate = c.getTime();
        System.out.println("aDate:plus 60:" + aDate);
        c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 90);
        aDate = c.getTime();
        System.out.println("aDate:plus 90:" + aDate);
        c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 120);
        aDate = c.getTime();
        System.out.println("aDate:" + aDate);
        c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 360);
        aDate = c.getTime();
        System.out.println("aDate:" + aDate);
    }

}
